/**
 * @file GetNewMessagesHandler.cs
 * @author Zane Mayo
 * @date 7/5/2009
 */

using System;
using System.Collections.Generic;

namespace MxitAPI {
	
	
	public class GetNewMessagesHandler : Handler {
		
		private static log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
		
		public GetNewMessagesHandler() : base( typeof( GetNewMessagesProtocolMessage ) ) {
		}
		
		protected override void HandleSpecificResponseProtocolMessage( ProtocolMessage protocolMessage ) {
			GetNewMessagesProtocolMessage getNewMessagesMessage =  protocolMessage as GetNewMessagesProtocolMessage;
			if ( getNewMessagesMessage == null ) 
				throw new MxitException( "Could not handle response. The message is of the wrong type." );
			
			//List<RawMessage> rawMessages =  getNewMessagesMessage.GetRawMessages();
			RawMessage rawMessage = getNewMessagesMessage.GetRawMessage();
			//log.Debug( "Processing raw messages." );
			//foreach ( RawMessage rawMessage in rawMessages ) {

			if ( rawMessage != null ) {
				ChatSessions.Instance.AddNewMessage( rawMessage );
				log.Debug( "Processing raw message." );
				log.Debug( rawMessage.MessageText );
				log.Debug( rawMessage );
			}

            ChatSessions.Instance.DoNewMessagesReceived();
			//}
		}		
		
	}
}
